package com.my.zidingyikongjian.view.six;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * 项目名称：可动贝塞尔曲线
 * 创建人：mwb
 * 创建时间：2018/7/24 8:27
 */

public class MwbBezier extends View {
    private Path mPath;
    private Paint mPaint;
    private Paint mPaintTwo;
    /**
     * 项目波长
     */
    private int mItemWaveLength = 1000;
    private int dx;
    private int dy;
    private int dxTwo;


    public MwbBezier(Context context) {
        super(context);
    }

    public MwbBezier(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPath = new Path(); // 画布
        mPaint = new Paint(); // 画笔
        mPaint.setColor(0x65E81010); // 画笔颜色
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE); // 画笔填充

        mPaintTwo = new Paint();
        mPaintTwo.setColor(0x65E81010);
        mPaintTwo.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPath.reset(); // 重置
        int originY = 300; // 起点Y
        int halfWaveLen = mItemWaveLength / 2; // 半波波长
        mPath.moveTo(-mItemWaveLength - dx, originY + dy);// 将mPath的起始位置向左移一个波长 波浪方向 <——

        for (int i = -mItemWaveLength; i <= getWidth() + mItemWaveLength; i += mItemWaveLength) {
            mPath.rQuadTo(halfWaveLen / 2, -100, halfWaveLen, 0);
            mPath.rQuadTo(halfWaveLen / 2, 100, halfWaveLen, 0);
        }

        mPath.lineTo(getWidth(), getHeight());
        mPath.lineTo(0, getHeight());
        mPath.close();

        canvas.drawPath(mPath, mPaint);


        mPath.reset(); // 重置
        int originYTwo = 300; // 起点Y
        int halfWaveLenTwo = mItemWaveLength / 2; // 半波波长
        mPath.moveTo(-mItemWaveLength - dxTwo + 200, originYTwo + dy);// 将mPath的起始位置向左移一个波长 波浪方向 <—— // 神奇的200

        for (int i = -mItemWaveLength; i <= getWidth() + mItemWaveLength; i += mItemWaveLength) {
            mPath.rQuadTo(halfWaveLenTwo / 2, 100, halfWaveLenTwo, 0);
            mPath.rQuadTo(halfWaveLenTwo / 2, -100, halfWaveLenTwo, 0);
        }

        mPath.lineTo(getWidth(), getHeight());
        mPath.lineTo(0, getHeight());
        mPath.close();

        canvas.drawPath(mPath, mPaintTwo);
    }

    public void startAnim() {
        // 赋值动画
        ValueAnimator animator = ValueAnimator.ofInt(0, mItemWaveLength);
        animator.setDuration(5000); // 动画时间
        animator.setRepeatCount(ValueAnimator.INFINITE); // 设置动画的重复次数
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                dx = (int) animation.getAnimatedValue();
                dxTwo = (int) animation.getAnimatedValue() * 2; // 线速度
                postInvalidate();
            }
        });
        animator.start();
    }

}
